package com.blockmeta.bbs.immodule.widget;

import android.content.Context;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.Group;
import android.text.Layout;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blockmeta.bbs.baselibrary.utils.LoadPicUtil;
import com.blockmeta.bbs.baselibrary.utils.TimeUtil;
import com.blockmeta.bbs.businesslibrary.util.RouterActivityHelper;
import com.blockmeta.bbs.businesslibrary.widget.CircleImageView;
import com.blockmeta.bbs.immodule.R;
import com.blockmeta.bbs.immodule.corebean.Impl.IMApplicationContext;
import com.blockmeta.bbs.immodule.dao.IMUserEntity;
import com.blockmeta.bbs.immodule.utils.IMUtils;

import rx.functions.Action0;

public class PersonInfoBar extends LinearLayout {
    Context mContext;

    private ConstraintLayout clayoutCircleOwnerInfo;
    private CircleImageView contactsAvatar;
    private TextView contactsName;
    private TextView tvOwnerDesc;
    private ImageView ivArrowOwnerDesc;
    private ImageView ivIcon;
    private TextView contactsGrade;
    private TextView contactsRegistrationTime;
    private Group groupExpand;
    private TextView tvOwnerLastActive;



    public PersonInfoBar(Context context) {
        super(context);
        init(context);
    }

    public PersonInfoBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PersonInfoBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(final Context context){
        inflate(context, R.layout.circle_owner_info,this);

        clayoutCircleOwnerInfo = (ConstraintLayout) findViewById(R.id.clayout_circle_owner_info);
        contactsAvatar = (CircleImageView) findViewById(R.id.contacts_avatar);
        contactsName = (TextView) findViewById(R.id.contacts_name);
        tvOwnerDesc = (TextView) findViewById(R.id.tv_owner_desc);
        ivArrowOwnerDesc = (ImageView) findViewById(R.id.iv_arrow_owner_desc);
        ivIcon = (ImageView) findViewById(R.id.iv_icon);
        contactsGrade = (TextView) findViewById(R.id.contacts_grade);
        contactsRegistrationTime = (TextView) findViewById(R.id.contacts_registration_time);
        groupExpand = findViewById(R.id.group_expand);
        tvOwnerLastActive = findViewById(R.id.tv_owner_last_active);
    }

    public void onInitialize(Context context, final IMUserEntity item, long circleId, long ownerLastMsgTime, final Action0 clickAction) {
        mContext = context;

        clayoutCircleOwnerInfo.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                RouterActivityHelper.openUserCenter(mContext, String.valueOf(item.getUserId()));
            }
        });

        LoadPicUtil.loadWithPicasso(contactsAvatar, item.getAvatarPath());

        contactsName.setText(IMApplicationContext.getCircleDomainService().getCircleRemarkName(item, circleId));

        if(ownerLastMsgTime == 0){
            tvOwnerLastActive.setVisibility(GONE);
        }else {
            String lastActiveTimeStr = TimeUtil.getTimeFormatText((int) (ownerLastMsgTime ));
            tvOwnerLastActive.setText("最后活跃: " + lastActiveTimeStr);
        }

        if(!TextUtils.isEmpty(item.getSignature())){
            groupExpand.setVisibility(GONE);
            tvOwnerDesc.setVisibility(VISIBLE);
            tvOwnerDesc.setText(item.getSignature());

            ViewTreeObserver vto = tvOwnerDesc.getViewTreeObserver();
            vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    Layout l = tvOwnerDesc.getLayout();
                    if (l != null) {
                        int lines = l.getLineCount();
                        if (lines > 0) {
                            if (l.getEllipsisCount(lines - 1) > 0) {
                                ivArrowOwnerDesc.setVisibility(View.VISIBLE);
                                ivArrowOwnerDesc.setOnClickListener(new View.OnClickListener(){
                                    @Override
                                    public void onClick(View v) {
                                        if(clickAction != null){
                                            clickAction.call();
                                        }
                                    }
                                });
                            }else{
                                ivArrowOwnerDesc.setVisibility(View.GONE);
                            }
                        }
                    }
                }
            });

        }else {
            groupExpand.setVisibility(VISIBLE);
            tvOwnerDesc.setVisibility(GONE);
            contactsGrade.setText(IMUtils.UserLevel.getUserLevel(item.getUserLevel()).getValue());
            contactsRegistrationTime.setText("加入 " +TimeUtil.getUserRegisterAge(item.getDate()));
        }

    }

}
